/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.account;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.account.SysMenu;
import com.unis.model.account.SysUser;

/**
 * @author CSJ (email:raulcsj@163.com)
 * @since 2012-12-10
 */
@Service("sysmenuService")
public class SysMenuServiceImpl implements SysMenuService {
    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of SysMenu
     ****************************************/
    public void insert(SysMenu sysmenu) throws Exception {
        sysmenu.setMenuid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(sysmenu);
    }

    /****************************************
     * Update a instace of SysMenu
     ****************************************/
    public void update(SysMenu sysmenu) throws Exception {
        iDataSet4iBatis.update(sysmenu);
    }

    /****************************************
     * Delete a instace of SysMenu
     ****************************************/
    public void delete(SysMenu sysmenu) throws Exception {
        iDataSet4iBatis.delete(sysmenu);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(SysMenu sysmenu) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(SysMenu sysmenu) throws Exception {
        String statementId = sysmenu.getStatmentId();
        String mainId = sysmenu.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> sysmenuList = new ArrayList<BaseModel>();
        for (String id : ids) {
            SysMenu model = new SysMenu();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            sysmenuList.add(model);
        }
        iDataSet4iBatis.batchDelete(sysmenuList);
    }

    /****************************************
     * Update instaces of SysMenu
     ****************************************/
    public void batchUpdate(List<LinkedHashMap<?, ?>> sysmenus) throws Exception {
        String statmentId = "SysMenu.update";
        List<BaseModel> menuList = new ArrayList<BaseModel>();
        for (LinkedHashMap<?, ?> linkedHashMap : sysmenus) {
            SysMenu sysMenu = new SysMenu();
            BeanUtils.copyProperties(sysMenu, linkedHashMap);
            sysMenu.setStatmentId(statmentId);
            menuList.add(sysMenu);
        }
        iDataSet4iBatis.batchUpdate(menuList);
    }

    /****************************************
     * Query instaces of SysMenu
     ****************************************/
    public List<BaseModel> queryAsList(SysMenu sysmenu) throws Exception {
        String queryListStatement = "";
        if (sysmenu.getMenuid() == null || "".equals(sysmenu.getMenuid())) {
            sysmenu.setMenuid("0");
        }
        if (sysmenu.isPaging()) {
            queryListStatement = sysmenu.getStatmentId();
            sysmenu.setStatmentId("SysMenu.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(sysmenu);
            sysmenu.setTotalCount(baseModel.getTotalCount());
            sysmenu.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(sysmenu);
    }

    /****************************************
     * Query SysMenus for login user
     ****************************************/
    public List<BaseModel> querySysUserMenus(SysUser sysUser) throws Exception {
        sysUser.setStatmentId("SysMenu.queryMenus4User");
        return iDataSet4iBatis.queryAsList(sysUser);
    }

    /****************************************
     * Query a instaces of SysMenu
     ****************************************/
    public BaseModel queryAsObject(SysMenu sysmenu) throws Exception {
        return iDataSet4iBatis.queryAsObject(sysmenu);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }

}
